﻿using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public Form4(int id, string title, string author,string createdTime)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
            textBox3.Text = createdTime;
        }
        public int id;
        private void button1_Click(object sender, EventArgs e)
        {
            //确定
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    var sql = string.Format("update Artycles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id={3}", textBox1.Text, textBox2.Text, textBox3.Text,this.id);

                    var res = DnHelper.AddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("更新成功！");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                }

            }
            else
            {
                var title = textBox1.Text;
                var author = textBox2.Text;
                var createdTime = textBox3.Text;
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Artycles (Title,Author,CreatedTime) values ('{0}','{1}','{2}')", title, author,createdTime);

                    var res = DnHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败。");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("文章名称不能为空");
                }
            }
            //if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
            //{
            //    MessageBox.Show("信息不能为空");
            //}
            //else
            //{
            //    string sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where id='{3}'", textBox1.Text, textBox2.Text, textBox3.Text, id);
            //    var get = DnHelper.AddOrEditOrDeleteSave(sql);
            //    if (get >= 0)
            //    {
            //        MessageBox.Show("更新成功", "提示");
            //        this.DialogResult = DialogResult.Yes;
            //        this.Close();
            //    }
            //}
        }


        private void button2_Click(object sender, EventArgs e)
        {
            //取消
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}
